{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'img' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-9d31776a6a94>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      8\u001b[0m     \u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m255\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m255\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m255\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m128\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m128\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m128\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m ],dtype = np.uint8)\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimsave\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'img_pyplot.jpg'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mimg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'img_cv2.jpg'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mimg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'img' is not defined"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "im = np.array([\n",
    "    [[255, 0, 0], [0, 255, 0], [0, 0, 255]],\n",
    "    [[255, 255, 0], [255, 0, 255], [0, 255, 255]],\n",
    "    [[255, 255, 255], [128, 128, 128], [0, 0, 0]],\n",
    "],dtype = np.uint8)\n",
    "plt.imsave('img_pyplot.jpg',img)\n",
    "cv2.imwrite('img_cv2.jpg',img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "img = cv2.imread('image0.JPG',1)\n",
    "cv2.imshow('image',img)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows() # important part!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "图片写入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "img = cv2.imread('image0.jpg',1)\n",
    "cv2.imwrite('image5.jpg',img)#1name2,src"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "图片质量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "img = cv2.imread('image0.jpg',1)\n",
    "cv2.imwrite('imageTest.jpg',img,[cv2.IMWRITE_JPEG_QUALITY,50])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "img = cv2.imread('image0.jpg',1)\n",
    "cv2.imwrite('imageTest.PNG',img,[cv2.IMWRITE_PNG_COMPRESSION,5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.像素，2.RGB,3,像素深度4，wh像素点5.720*547*3*8 6.RGBalpha"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "像素操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "39 255 140\n"
     ]
    }
   ],
   "source": [
    "## import cv2\n",
    "import numpy as np\n",
    "img = cv2.imread('image5.png',1)\n",
    "\n",
    "print(b,g,r)\n",
    "# 转换换到 HSV（原图是BGR类型，也就是常说的RGB）\n",
    "hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n",
    "(b,g,r)=hsv[4,4]\n",
    "\n",
    "\n",
    "# 设定蓝色的阀值（可以通过调色板等工具获取到，后面会介绍另一种方法动态获得最适合当前图片的阀值）\n",
    "lower_blue=np.array([0,140,100]) \n",
    "upper_blue=np.array([0,145,110])\n",
    "# 根据阀值构建掩模 （类似有一张黑色的纸，根据上方颜色阀值在纸上扣出白色的区域）\n",
    "mask=cv2.inRange(hsv,lower_blue,upper_blue)\n",
    "cv2.imshow('res',mask)\n",
    "cv2.waitKey()\n",
    "# 对原图像和掩模位运算 （2张图片叠在一起，透过掩膜上的白色洞去看的内容就是结果）\n",
    "res=cv2.bitwise_and(img,img,mask=mask)\n",
    "# 图片展示\n",
    "cv2.imshow('res',res)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows() # important part!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30 32 26\n"
     ]
    }
   ],
   "source": [
    "## import cv2\n",
    "img = cv2.imread('image0.JPG',1)\n",
    "(b,g,r)=img[2,2]\n",
    "print(b,g,r)\n",
    "# 10 100 ---110 100\n",
    "for i in range(1,101):\n",
    "    img[10+i,100]=(0,140,100)\n",
    "\n",
    "cv2.imshow('image',img)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows() # important part!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
